<?php
ini_set('error_reporting', E_ALL);

$plugin_info = array(
  'pi_name' => 'File info',
  'pi_version' => '1.0',
  'pi_author' => 'Tom Kiss',
  'pi_author_url' => 'http://www.tomkiss.net',
  'pi_description' => 'Get selected ID3 file info for a given file.',
  'pi_usage' => Fileinfo::usage()
  );

class Fileinfo {

	var $return_data = "";
	
	function search_array($array, $text) {
		$output = "";
		if(is_array($array)){
			foreach($array as $name=>$value) {
				if(is_array($value)) {
					$text = $this->search_array($value, $text);
				} else {
					$text = str_replace("{".$name."}", strval($value), $text);
				}
			}
		}
		return $text;
	}
	
	function Fileinfo() {
		global $TMPL;
		$output = "";	
    	$filename = (!$TMPL->fetch_param('file')) ? '' : $TMPL->fetch_param('file');
		$output = (!$TMPL->tagdata) ? '' : $TMPL->tagdata;
		if ($filename != "") {
			require_once('getid3/getid3.php');
			$getID3 = new getID3;
			$filename = $_SERVER['DOCUMENT_ROOT'].$filename;
				//$filename = $_SERVER['DOCUMENT_ROOT']."/eu/images/uploads/sample.mp3"; //TEST
			if (file_exists($filename)) {
				// Get Info			
				$ThisFileInfo = $getID3->analyze($filename);
				if(is_array($ThisFileInfo)){
					getid3_lib::CopyTagsToComments($ThisFileInfo);
					$output = $this->search_array($ThisFileInfo, $output);
				}
			}
		}
		$this->return_data = $output;
	}


	function formatsize() {
		global $TMPL;
		//Size must be bytes!
    	$round = (!$TMPL->fetch_param('round')) ? false : $TMPL->fetch_param('round');
    	$size = (!$TMPL->fetch_param('filesize')) ? 0 : $TMPL->fetch_param('filesize');
		if(!isset($round) || !is_numeric($round)) {
			$round=0;
		}
		$sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
		$i = 0;
		while($size>1024) {
			$size/=1024;
			++$i;
		}
		return round($size,$round).' '.$sizes[$i];
	}
	
	function formatbitrate() {
		global $TMPL;
		//Size must be bytes!
    	$round = (!$TMPL->fetch_param('round')) ? false : $TMPL->fetch_param('round');
    	$bitrate = (!$TMPL->fetch_param('bitrate')) ? 0 : $TMPL->fetch_param('bitrate');
		if(!isset($round) || !is_numeric($round)) {
			$round=0;
		}
		$sizes = array('Bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps');
		$i = 0;
		while($bitrate>1024) {
			$bitrate/=1024;
			++$i;
		}
		return round($bitrate,$round).' '.$sizes[$i];
	}

  function usage() {
  	ob_start(); 
  ?>

This plugin will search through all of the available embedded ID3 information in a file, and parse EE tags matching the information.

{exp:fileinfo file="myfile.mp3"}
{title}
{artist}
{playtime_string}
{filesize}
{bitrate}
{filenamepath}
{/exp:fileinfo}

More information about ID3 tags  available can be found at:
http://getid3.sourceforge.net/

This plugin is relatively basic so not all ID3 tags will parse correctly.

<?php

		$buffer = ob_get_contents();
		ob_end_clean(); 
		return $buffer;
	}
  	// END
}

?>